package org.jeecg.modules.cshbasic.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bestvike.linq.Linq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.cshbasic.entity.CshStoreEntity;
import org.jeecg.modules.cshbasic.service.ICshStoreEntityService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: csh_store
 * @Author: zhaodui
 * @Date:   2020-04-07
 * @Version: V1.0
 */
@RestController
@RequestMapping("/csh/cshstore")
@Slf4j
@Api(tags = "门店权限")
public class CshStoreEntityController extends JeecgController<CshStoreEntity, ICshStoreEntityService> {

	@Autowired
	private ICshStoreEntityService cshStoreService;

    /**
     * 分页列表查询
     *
     * @param cshStore
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation("门店权限分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<?> queryPageList(CshStoreEntity cshStore,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CshStoreEntity> queryWrapper = QueryGenerator.initQueryWrapper(cshStore, req.getParameterMap());
        Page<CshStoreEntity> page = new Page<CshStoreEntity>(pageNo, pageSize);
        IPage<CshStoreEntity> pageList = cshStoreService.page(page, queryWrapper);

        return Result.ok(pageList);
    }

	/**
	 * 添加
	 *
	 * @param cshStore
	 * @return
	 */
	@ApiOperation("门店权限添加")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<?> add(@RequestBody CshStoreEntity cshStore) {
		cshStoreService.save(cshStore);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param cshStore
	 * @return
	 */
	@ApiOperation("门店权限编辑")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Result<?> edit(@RequestBody CshStoreEntity cshStore) {
		cshStoreService.updateById(cshStore);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation("门店权限删除")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		cshStoreService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation("门店权限批量删除")
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.cshStoreService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation("门店权限查询")
	@RequestMapping(value = "/queryById", method = RequestMethod.GET)
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		CshStoreEntity cshStore = cshStoreService.getById(id);
		if (cshStore == null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(cshStore);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param cshStore
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, CshStoreEntity cshStore) {
		return super.exportXls(request, cshStore, CshStoreEntity.class, "门店管理");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, CshStoreEntity.class);
	}

}
